<?php
/**
 * guide.php
 *
 * guide
 * @author konakona
 * @date 2009-10-9
 */
class guide {
	/**
	 * 添加guide
	 * @param string $_post
	*/
	public static function guide_add($_post){
		if(strlen($_post['title'])<5){
			k::message('标题不能少于5个英文字符','javascript:history.go(-1);');
		}
		if(strlen($_post['content'])<10){
			k::message('内容不能少于10个字符','javascript:history.go(-1);');
		}
		$sql = "INSERT INTO guide values('','{$_post[title]}','{$_post[content]}',".time().",0,'{$_post[district]}')";
		mysql::connect()->query($sql);
		k::message('成功添加Guide!!','javascript:history.go(-1);');
	}
	/**
	 * 删除Guide
	 * @param mixed $id
	*/
	public static function guide_del($id){
		$sql = "DELETE FROM guide WHERE gid=$id";
		mysql::connect()->query($sql);
		k::message('这条Guide已删除成功','javascript:history.go(-1);');
	}
	/**
	 * 列出所有Guide
	 * @param int $pid $dis_id
	 * @return array
	*/
	public static function guide_list($pid,$dis_name=false,$row=10){
		if($pid == 0){
			$pid = 1;
		}
		$allcount = page::tableCount('guide');
		if($dis_name==false){
			if($pid==1){
				$sql = "select * from guide  ORDER BY gid DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select * from guide limit  ORDER BY gid DESC limit ".($page_start-1).",$row";
			}
		}else{
			if($pid == 1){
				$sql = "select district.name,guide.gid,guide.clicknum,guide.dis_id,guide.title,guide.content from guide,district where district.name = '{$dis_name}' and guide.dis_id=district.id ORDER BY gid DESC limit ".($pid-1).",$row";
			}else{
				$page_start  = ($pid-1)+$row;
				$sql = "select district.name,guide.gid,guide.clicknum,guide.dis_id,guide.title,guide.content from guide,district where district.name = '{$dis_name}' and guide.dis_id=district.id ORDER BY gid DESC limit ".($page_start-1).",$row";
			}
		}
		return mysql::connect()->fetch_all($sql);
	}
	/**
	 * 读取一条guide信息
	 * @param int $id
	 * @return array
	*/
	public static function guide_details($id){
		$sql = "select district.name,guide.gid,guide.dis_id,guide.title,guide.addtime,guide.content from guide,district where guide.gid=$id and district.id = guide.dis_id";
		$result = mysql::connect()->fetch_all($sql);
		if($result[0]==''){
			k::message("Sorry,Can 't find No.{$id} guide.");
		}
		return $result[0];
	}
	/**
	 * 给一条guide增加点击率
	 * @param int $id
	*/
	public static function guide_clicknum($id){
		mysql::connect()->query("UPDATE `guide` SET `clicknum` = `clicknum`+1 WHERE `gid` =$id");
	}
	/**
	 * 修改一条guide信息
	 * @param int $_post
	*/
	public static function guide_edit($_post){
		$sql = "UPDATE `guide` SET `title` = '{$_post[title]}', `content` = '{$_post[content]}',`dis_id` = '{$_post[district]}' where gid={$_post[id]}";
		mysql::connect()->query($sql);
		k::message('修改成功','?action=guide-list');
	}
}